<?php
/*
 * scrumbrella - Scrum board.
 *
 * Copyright (C) 2009  Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/*!
 * \file
 * \brief Sprint scrumbrella API.
 */

require_once("db_schema.php");

/*!
 * \brief Delete a sprint backlog history based on the project id.
 * \param id The project id.
 * \retval 0 on success.
 * \retval < 0 on error.
 */
function sprint_backlog_history_delete_byproject($id)
{
	global $sprint_backlog_history_table;

	$res = db_query("DELETE FROM $sprint_backlog_history_table WHERE id_project = '$id'");
	if (db_error($res)) {
		logger_error("Unable to delete sprint backlog history with project id '$id': " . db_error_message($res));
		return -1;
	}

	return 0;
}

/*!
 * \brief Get the total number of sprint backlogs for a specified project.
 * \param project_id
 * \returns The number of sprint backlogs.
 */
function sprint_backlogs_total($project_id)
{
	global $sprint_backlog_table;
	global $project_backlog_table;

	$query = "SELECT count(DISTINCT sbt.id_pbacklog) AS total FROM 
		 $sprint_backlog_table AS sbt, $project_backlog_table AS pbt WHERE 
		 sbt.id_pbacklog = pbt.id_pbacklog AND pbt.id_project = '$project_id'";

	$result = db_query($query);
	if (db_error($result) || !db_numrows($result)) {
		logger_error("Unable to get the number of sprint backlogs for project $project_id: 
			     " . db_error_message($result));
		return 0;
	}

	$data = db_fetchrow($result);
	return $data["total"];
}

/*! \brief return the next sprint number
 *  \param project the project id
 *  \retval < 0 error
 *  \retval > 0 sprint number
 */
function sprint_newnumber($project)
{
	global $sprint_table;

	$SQL = "SELECT max(number) as number FROM $sprint_table WHERE id_project=$project";
	$res = db_query($SQL);

	if (db_error($res)) {
		logger_error("Unable to get max sprint number $project:" . db_error_message($res));
		return -1;
	}

	if (db_numrows($res) > 0) {
		$data = db_fetchrow($res);
		if (is_null($data['number'])) {
			return 1;
		} else {
			return $data["number"];
		}
	} else {
		return 1;
	}
}

/*! \brief create new sprint
 *  \param project project id where it depends
 *  \param number sprint number
 *  \param duration the duration in $unit of sprint
 *  \param beginning start date
 *  \param unit the time unit of duration
 *  \retval < 0 error
 *  \retval > 0 new sprint id
 */
function sprint_create($project,$number, $duration,$beginning,$unit)
{
	// TODO ADD column id_unit referenced to unit table
	global $sprint_table;

	$SQL = "INSERT INTO $sprint_table(id_project,number,date_beginning,duration) 
		VALUES ($project,$number,'$beginning',$duration);";
	$res = db_query($SQL);

	if (db_error($res)) {
		logger_error("Unable to create new sprint: " . db_error_message($res));
		return -1;
	}

	$SQL = "SELECT id_sprint FROM $sprint_table WHERE number=$number";
	$res = db_query($SQL);

	$data = db_fetchrow($res);
	return $data['id_sprint'];
}


/*! \brief return the user stories no planned for an sprint
 *  \param $sprint ID sprint
 *  \param $project ID project
 *  \retvak < 0 Error
 *  \retval Array
 */
function sprint_get_stories($sprint, $project, $status) {
	global $sprint_backlog_table;
	global $sprint_backlog_item_status_table;

	$SQL = "SELECT
			a.id_sbacklog,
			a.summary,
			a.description,
			a.priority,
			a.estimated,
			d.name,
			d.lastname,
			b.closed
		FROM
			sprint_backlog a,
			sprint_backlog_item_status b,
			project_backlog c,
			user d
		WHERE
			    b.name        = '$status'
			and c.id_project  = $project
			and a.id_sprint   = $sprint
			and a.id_pbacklog = c.id_pbacklog
			and a.id_status   = b.id_status
			and c.id_user     = d.id_user
		ORDER BY 
			a.priority DESC";
	$res = db_query($SQL);

	if (db_error($res)) {
		logger_error(" Error while get the user story " . db_error_message($res));
		return -1;
	}
	
	if (db_numrows($res) > 0) {
		$story_count = 0;
		while ($row = db_fetchrow($res)) {
			foreach ($row as $key => $value) {
				$stories[$story_count][$key] = $value;
			}
			$story_count++;
		}
		db_freeresult($res);
		return $stories;
	} else {
		return 0;
	}
}

/*! \brief return the sprint list 
 *  \param $project ID
 *  \retval < 0 Error
 *  \retval Array with sprint list
 */
function sprint_list_by_project($project) {
	global $sprint_table;

	$SQL = "SELECT * FROM $sprint_table WHERE id_project=$project";
	$res = db_query($SQL);
	
	if (db_error($res)) {
		logger_error(" Error while get sprint list " . db_error_message($res));
		return -1;
	}

	if (db_numrows($res) > 0) {
		$sprint_count = 0;
		while ($row = db_fetchrow($res)) {
			foreach ($row AS $key => $value) {
				$sprints[$sprint_count][$key] = $value;
			}
			$sprint_count++;
		}
		db_freeresult($res);
		return $sprints;
	} else {
		return 0;
	}
}

/*! \brief return status list for user stories
 *  \retval < 0 ERROR
 *  \retval Array 
 */
function sprint_status_list()
{
	global $sprint_backlog_item_status_table;

	$SQL = "SELECT * FROM $sprint_backlog_item_status_table";
	$res = db_query($SQL);

	if (db_error($res)) {
		logger_error(" Error while get status list " . db_error_message($res));
		return -1;
	}

	if (db_numrows($res) > 0) {
		$status_count = 0;
		while ($row = db_fetchrow($res)) {
			foreach ($row AS $key => $value) {
				$status[$status_count][$key] = $value;
			}
			$status_count++;
		}
		db_freeresult($res);
		return $status;
	} else {
		return 0;
	}
}

/*! \brief create a sprint backlog
 *  \param $pbacklog ID of project backlog
 *  \param $status status ID
 *  \param $unit ID of unit time
 *  \param $estimated int estimated time
 *  \param $sprint id
 *  \retval < 0 ERROR
 *  \retval 1 Ok
 */
function sprint_create_backlog ($pbacklog, $status, $unit, $estimated, $sprint)
{
	global $sprint_backlog_table;

	$backlog = project_backlog_detail($pbacklog);

	$SQL = "INSERT INTO $sprint_backlog_table
		(id_status,id_pbacklog,id_unit,summary,description,last_modified,priority,estimated,id_sprint) 
		VALUES
		($status,$pbacklog,$unit,'{$backlog[0]['summary']}', '{$backlog[0]['description']}','".date("y-m-d h:i:s")."', 
		{$backlog[0]['priority']},$estimated,$sprint)";
	$res = db_query($SQL);

	$unit_name = unit_name($unit);
	$user_name = user_name($backlog[0]['id_user']);

	project_backlog_to_history($pbacklog,$unit_name,$backlog[0]['summary'],$backlog[0]['description'],$backlog[0]['priority'],$estimated,$user_name);

	if (db_error($res)) {
		logger_error(" Error while create user story " . db_error_message($res));
		return -1;
	}

	return 1;
}
?>
